<!-- 任务-门店新增审批-->
<template>
  <div class="addExamine">
    <CommonBtn label-name="新增门店审核列表" v-if="!button || (button.event!='add()' && button.event!='edit()') " :buttons="buttons" @onButtonClick="onButtonClick">
      <span slot="conditions" v-for="(item,index) of conditions" :key="index" v-if="item.checked && item.label && item.valueName != undefined">{{item.label+":"+item.valueName}}</span>
    </CommonBtn>
    <CommonList ref="list" v-if="!showEdit" :buttons="buttons" :columns="columns" :listUrl="listUrl" :conditions="conditions" @tableSelect="tableSelect" @selectAll="selectAll" @dbClick="openItem" showSelect :selectChange="selectCheckbox"></CommonList>
    <StoreEdit :pageType="pageType"
                  ref="storeEdit"
                  :disabled="!enableEdit"
                  :label="editTitle"
                  v-if="showEdit"
                  :items="items"
                  :originalData="row"
                  @editBack="editBack"
                  @search="search"
                  @submit="submit"
                  @approvalPass="approvalPass"
                  @approvalBack="approvalBack"/>
    <CommonSearch :label="item.label"
                  :conditions="item.conditions"
                  v-for="(item, index) of searchs"
                  :key="index"
                  :listUrl="item.listUrl"
                  :columns="item.columns"
                  :items="item.items"
                  :page="item.page"
                  :originalData="item.searchData"
                  @search="search"
                  @submit="item.selectItem"
                  @searchBack="searchBack"/>
    <CommonExport :out-btn="button" :props="columns" :export_type="export_type"
                  @close="close" :export-url="$api.API_RETAILER_EXPORT" :conditions="conditions"></CommonExport>
    <el-dialog title="驳回理由" :visible.sync="batchapprovalBackReasonVisible" :modal-append-to-body='false'>
      <el-input type="textarea" autocomplete="off" v-model="batchapprovalBackReason" placeholder="请输入驳回理由"/>
      <div slot="footer" class="dialog-footer">
        <el-button @click="batchapprovalBackReasonVisible = false">取 消</el-button>
        <el-button type="primary" @click="batchapprovalBack">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import http from '@/api/http'
import {Message } from 'element-ui'
    import CommonList from "../../common/CommonList"
    import CommonBtn from "../../common/CommonBtn";
    import CommonSearch from '../../common/CommonSearch'
    import StoreEdit from "./StoreEdit";
    import CommonExport from "../../common/CommonExport";
    export default {
        name:'addExamine',
        data(){
            return{
              batchapprovalBackReasonVisible:false,
              batchapprovalBackReason: '',
              searchData: {},
              searchItems: [],
              searchs:[],
              listUrl:this.$api.API_SELECT_RETAILER_LIST,
              button:null,
              conditions:[{
                  checked: true,
                  prop: 'pageType',
                  value: '2'
              }],
              export_type: [{
                  checked: true,
                  prop: 'pageType',
                  value: '2'
              }],
              row: null,
              selectCheckbox: [],
              pageType: 2,
              showEdit: false,
              enableEdit: false,
              editTitle: '',
                buttons:[
                    this.$config.BTNS.EXPORT,
                    this.$config.BTNS.SEARCH,
                    this.$config.BTNS.BATCHAUDITTHROUGH,
                    this.$config.BTNS.BATCHAUDITREJECTED
                ],
                columns:[
                    {
                        prop:'mdmCode',
                        label:'统一编码'
                    },
                    {
                        prop:'code',
                        label:'外部系统编码'
                    },
                    {
                        prop:'retailName',
                        label:'门店名称'
                    },
                    {
                        prop:'wfStatus',
                        label:'节点状态'
                    },
                    {
                        prop:'dataSource',
                        label:'数据来源'
                    },
                    {
                        prop:'retailGroup',
                        label:'所属集团客户'
                    },
                    {
                        prop:'isLKAvalue',
                        label:'是否LKA'
                    },
                    {
                        prop:'retailType',
                        label:'门店类型'
                    },
                    {
                        prop:'locationName',
                        label:'Location'
                    },
                    {
                        prop:'province',
                        label:'省'
                    },
                    {
                        prop:'cityName',
                        label:'市'
                    },
                    {
                        prop:'adminCity',
                        label:'区县'
                    },
                    {
                        prop:'deAddress',
                        label:'详细地址'
                    },
                    {
                        prop:'repName',
                        label:'负责人'
                    },
                    {
                        prop:'remark',
                        label:'Remark'
                    },
                    {
                        prop:'createTime',
                        label:'创建时间'
                    },
                    {
                        prop:'modifyTime',
                        label:'最后修改时间'
                    },
                ],
                items: [
                {
                    name: '统一编码',
                    key: 'mdmCode',
                    type: 'text',
                    disable: true,
                    required: false,
                    placeholder: '待生成'
                },
                {
                  name: '',
                  key: '',
                  type: 'empty',
                  disable: true,
                  required: false
                },
                {
                  name: '外部系统编码',
                  key: 'code',
                  type: 'checktext',
                  disable: false,
                  required: true,
                  placeholder: '',
                  checkname: '是否自动生成'
                },
                {
                  name: '门店名称',
                  key: 'retailName',
                  type: 'text',
                  disable: false,
                  required: true,
                  placeholder: ''
                },
                {
                  name: '地理城市',
                  key: 'city',
                  type: 'multi-select',
                  required: false,
                  optionss: [{
                    options: [],
                    key: 'province'
                  }, {
                    options: [],
                    key: 'physicalCity'
                  }, {
                    options: [],
                    key: 'adminCity'
                  }]
                },
                {
                  name: '城市类别',
                  key: 'cityTypeName',
                  vkey: 'fkCityType',
                  type: 'single-select',
                  required: false,
                  disable: true,
                  options: []
                },
                {
                  name: '详细地址',
                  key: 'deAddress',
                  type: 'multi-text',
                  required: false,
                  texts: [{
                    key: 'address',
                    name: '街道',
                    span: 12
                  }, {
                    key: 'address1',
                    name: '路',
                    span: 12
                  }, {
                    key: 'address2',
                    name: null,
                    span: 24
                  }]
                },
                {
                  name: '经纬度',
                  type: 'location',
                  key1: 'longitude',
                  key2: 'latitude',
                  disable: true,
                },
                {
                  name: '门店类型',
                  key: 'retailType',
                  vkey: 'fkRetailType',
                  type: 'single-select',
                  required: false,
                  disable: false,
                  options: []
                },
                {
                  name: 'Location',
                  key: 'locationName',
                  vkey: 'fkLocation',
                  type: 'single-select',
                  required: false,
                  disable: true,
                  placeholder: '请选择'
                },
                {
                  name: '门店投资属性',
                  key: 'levelName',
                  vkey: 'fkLevel',
                  type: 'single-select',
                  required: false,
                  disable: true,
                  options: []
                },
                {
                  name: '所属客户集团',
                  key: 'retailGroup',
                  type: 'search',
                  required: false,
                  disable: false
                },
                {
                  name: '是否LKA',
                  key: 'isLKAvalue',
                  vkey: 'isLKA',
                  type: 'single-select',
                  required: true,
                  disable: false,
                  placeholder: '请选择',
                  options: []
                },
                {
                  name: '强生负责人',
                  key: 'repName',
                  type: 'search',
                  required: true,
                  disable: false
                },
                {
                  name: '门店联系人姓名',
                  key: 'linkMan',
                  type: 'text',
                  required: false,
                  disable: false
                },
                {
                  name: '门店联系人电话',
                  key: 'tel',
                  type: 'text',
                  required: false,
                  disable: false
                },
                {
                  name: '门店联系人邮箱',
                  key: 'retailerMail',
                  type: 'text',
                  required: false,
                  disable: false,
                  placeholder: '多个邮箱请用英文,(逗号分割)'
                },
                {
                  name: '门店营业年月',
                  key: 'businessTime',
                  type: 'date',
                  year: 'businessYear',
                  month: 'businessMonth',
                  options: [],
                  required: true,
                  disable: false,
                  placeholder: '请选择'
                },
                {
                  name: '营业状态',
                  key: 'businessStatus',
                  vkey: 'businessStatus',
                  type: 'single-select',
                  required: false,
                  disable: false,
                  placeholder: '请选择'
                },
                {
                  name: '合作状态',
                  key: 'cooperationStatus',
                  vkey: 'cooperationStatus',
                  type: 'single-select',
                  required: false,
                  disable: true,
                  placeholder: '请选择'
                },
                {
                  name: 'Destination',
                  key: 'destinationName',
                  vkey: 'fkDestination',
                  type: 'single-select',
                  required: false,
                  disable: true,
                  placeholder: '请选择'
                },
                {
                  name: 'Remark',
                  key: 'remark',
                  type: 'text',
                  required: false,
                  disable: false
                },
                {
                  name: '是否Promoter覆盖',
                  key: 'isPromoter',
                  type: 'checkbox',
                },
                {
                  name: '是否B2B生意覆盖',
                  key: 'isB2b',
                  type: 'checkbox',
                },
                {
                  name: '是否销售强生商品',
                  key: 'isSalesjnj',
                  type: 'checkbox',
                },
                ]
            }
        },
        methods:{
          onButtonClick(item) {
              this.enableEdit = true
              if (item.event == 'edit()') {
                if (!this.row || JSON.stringify(this.row) == '{}') {
                  this.$message.error("请选择一行数据")
                  return
                }
                this.editTitle = '编辑门店信息'
                this.showEdit = true
              }
              if (item.event == 'add()') {
                  this.showEdit = true
                  this.editTitle = '新增门店信息'
                  this.row = null
                  this.selectCheckbox = []
              }
              let self = this
              if (item.event == 'search()') {
                this.searchs.push({
                  label: '查询门店信息',
                  page: 'store',
                  searchData: this.searchData,
                  items: this.searchItems,
                  selectItem: function (formData, items) {
                    self.searchs.pop()
                    self.searchItems = items;
                    self.searchData = formData;
                    let conditions = self.conditions.filter(item=>{
                      return !item.label
                    })
                    // 回调
                    for (let idx in items) {
                      let item = items[idx]
                      if (item.checked == true) {
                        conditions.push({
                          checked: true,
                          label: item.name,
                          prop: item.vkey ? item.vkey : item.key,
                          valueName: formData[item.key],
                          value: item.vkey ? formData[item.vkey] : formData[item.key]
                        })
                      }
                    }
                    self.conditions = conditions
                  },
                })
                this.selectCheckbox = []
              }
              if (item.event == 'batchAuditThrough()') {
                if (!this.selectCheckbox || JSON.stringify(this.selectCheckbox) == '{}') {
                  this.$message.error("请选择一行数据")
                  return
                }
                var array = []
                for (var i = 0; i < this.selectCheckbox.length; i++) {
                  array.push(this.selectCheckbox[i].pkid)
                }
                
                this.$confirm('您确认要【批量通过】审核当前信息?', '系统提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).then(() => {
                  http.post(this.$api.API_STORE_CREATE_APPROVAL_PASS, {pkid: array.join(',')})
                      .then(resp=>{
                        Message({type: 'success', message: '已批量审核通过！'})
                        this.selectCheckbox = []
                        this.conditions = this.conditions.map(item=>{return item})
                      })
                }).catch(() => {});
              }
              if (item.event == 'batchAuditRejected()') {
                if (!this.selectCheckbox || JSON.stringify(this.selectCheckbox) == '{}') {
                  this.$message.error("请选择一行数据")
                  return
                }
                this.batchapprovalBackReasonVisible = true
                // var array = []
                // for (var i = 0; i < this.selectCheckbox.length; i++) {
                //   array.push(this.selectCheckbox[i].pkid)
                // }
                // this.$confirm('您确认要【批量驳回】审核当前信息?', '系统提示', {
                //   confirmButtonText: '确定',
                //   cancelButtonText: '取消',
                //   type: 'warning'
                // }).then(() => {
                //   http.post(this.$api.API_STORE_CREATE_APPROVAL_BACK, {pkid: array.join(',')})
                //       .then(resp=>{
                //         this.batchapprovalBackReasonVisible = false
                //         Message({type: 'success', message: '已批量审核驳回！'})
                //         this.showEdit = false
                //         this.selectCheckbox = []
                //         this.conditions = this.conditions.map(item=>{return item})
                //       })
                // }).catch(() => {});
              }
              this.button = item
          },
          search(item, callback) {
            let self = this
            if (item.key == 'repName') {
              // 客户属性
              this.searchs.push({
                label: '人员信息',
                conditions: [],
                listUrl: this.$api.API_GETTPINFO_LIST,
                selectItem: function (item) {
                  self.searchs.pop()
                  // 回调
                  if (typeof(callback) == 'function') {
                    callback({repName: item.repName, fkRep: item.pkid})
                  }
                },
                items: [{
                  name: '员工姓名',
                  key: 'repName',
                  type: 'text',
                },{
                  name: '员工账号',
                  key: 'repCode',
                  type: 'text',
                },{
                  name: '所属管理层级',
                  key: 'positionName',
                  type: 'search',
                },{
                  name: 'WWID',
                  key: 'wWID',
                  type: 'text',
                },{
                  name: 'SAP代码',
                  key: 'sapCode',
                  type: 'text',
                },{
                  name: '总部/大区',
                  key: 'hQOrRegion',
                  type: 'text',
                }],
                columns: [{
                  prop: 'repName',
                  label: '员工姓名'
                },{
                  prop: 'repCode',
                  label: '员工账号'
                },{
                  prop: 'positionName',
                  label: '所属管理层级'
                },{
                  prop: 'WWID',
                  label: 'wWID'
                },{
                  prop: 'sapCode',
                  label: 'SAP代码'
                },{
                  prop: 'hQOrRegion',
                  label: '总部/大区'
                }]
              })
            }
          },
          editBack() {
              this.showEdit = false
              this.selectCheckbox = []
              this.button = null
          },
          tableSelect(item) {
            var status = 0
            if (this.selectCheckbox.length > 0) {
              for (var i = 0; i < this.selectCheckbox.length; i++) {
                if (item.pkid == this.selectCheckbox[i].pkid) {
                  status = 1
                  this.selectCheckbox.splice(i, 1)
                  break;
                }
              }
              if (status == 0) {
                this.selectCheckbox.push(item)
              }
            } else {
              this.selectCheckbox.push(item)
            }
            this.button = null
          },
          selectAll (list, data) {
            if (list && list.length > 0 && this.selectCheckbox == null){
              this.selectCheckbox = JSON.parse(JSON.stringify(list)) 
            } else if (list && list.length > 0) {
              for (var i = 0; i < list.length; i++) {
                var status = 0
                for (var j = 0; j < this.selectCheckbox.length; j++) {
                  if (this.selectCheckbox[j].pkid == list[i].pkid) {
                    status = 1
                    break;
                  }
                }
                if (status == 0) {
                  this.selectCheckbox.push(list[i])
                }
              }
            } else if (data && data.length > 0){
              for (var i = 0; i < data.length; i++) {
                for (var j = 0; j < this.selectCheckbox.length; j++) {
                  if (this.selectCheckbox[j].pkid == data[i].pkid) {
                    this.selectCheckbox.splice(j, 1)
                    break;
                  }
                }
              }
            }
          },
          openItem(item) {
            // 打开详情
            this.showEdit = true
            this.row = item
            this.enableEdit = false
            this.editTitle = '查看门店信息'
          },
          searchBack() {
            this.searchs.pop()
          },
          submit(formData) {

          },
          close(item) {
              this.button = item
          },
          approvalPass(formData) {
            // 审核通过
            this.$confirm('您确认要【通过】审核当前信息?', '系统提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              http.post(this.$api.API_STORE_CREATE_APPROVAL_PASS, {pkid: formData.pkid})
                  .then(resp=>{
                    Message({type: 'success', message: '已审核通过！'})
                    this.showEdit = false
                    this.row = null
                    this.selectCheckbox = []
                  })
            }).catch(() => {});
          },
          approvalBack(formData,reason) {
            // 审核驳回
            this.$confirm('您确认要【驳回】审核当前信息?', '系统提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              http.post(this.$api.API_STORE_CREATE_APPROVAL_BACK, {pkid: formData.pkid,reason: reason})
                  .then(resp=>{
                    Message({type: 'success', message: '已审核驳回！'})
                    this.showEdit = false
                    this.row = null
                    this.selectCheckbox = []
                  })
            }).catch(() => {});
          },
          batchapprovalBack(formData,reason) {
            // 批量审核驳回
            var array = []
            for (var i = 0; i < this.selectCheckbox.length; i++) {
              array.push(this.selectCheckbox[i].pkid)
            }
            this.$confirm('您确认要【批量驳回】审核当前信息?', '系统提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              http.post(this.$api.API_STORE_CREATE_APPROVAL_BACK, {pkid: array.join(','),reason: this.batchapprovalBackReason})
                  .then(resp=>{
                    this.batchapprovalBackReasonVisible = false
                    Message({type: 'success', message: '已审核驳回！'})
                    this.showEdit = false
                    this.selectCheckbox = []
                    this.conditions = this.conditions.map(item=>{return item})
                  })
            }).catch(() => {});
          },
        },
        watch: {
          showEdit(newVal,oldVal) {
            if (!newVal && oldVal) {
              this.conditions = this.conditions.map(item=>{return item})
            }
          }
        },
        components:{CommonExport,CommonList,CommonBtn,StoreEdit, CommonSearch}
    }
</script>
